//
//  DateEventViewController.m
//  lvtu
//
//  Created by Apple on 12-3-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DateEventViewController.h"
#import "DateAddEventViewController.h"
#import "ShowDesiginViewController.h"
#import "NSDate+Helper.h"

@implementation DateEventViewController
@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [dateDetailView release];
    [super dealloc];
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.navigationItem.title = @"旅途设计";
    UILabel* tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    [tempLabel setText:@"旅途设计"];
    [tempLabel setBackgroundColor:[UIColor clearColor]];
    [tempLabel setTextAlignment:UITextAlignmentCenter];
    [tempLabel setTextColor:[UIColor colorWithRed:111/255.0 green:117/255.0 blue:81/255.0 alpha:1.0]];
    [tempLabel setFont:[UIFont boldSystemFontOfSize:20]];
    self.navigationItem.titleView = tempLabel;
    [tempLabel release];
    tempLabel = nil;
    
    self.tabBarController.view.hidden = YES;
    
    UIImageView* backgroudImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+300)];
    backgroudImageView.image = [UIImage imageNamed:@"bg_texture.png"];
    [self.view addSubview:backgroudImageView];
    [backgroudImageView release];
    
    UIImage* backImage = [UIImage imageNamed:@"btn_back_normal.png"];
    UIButton* backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    backbutton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
    [backbutton setBackgroundImage:backImage forState:UIControlStateNormal];
    [backbutton setBackgroundImage:[UIImage imageNamed:@"btn_back_press.png"] forState:UIControlStateHighlighted];
    [backbutton addTarget:self action:@selector(backButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
    self.navigationItem.leftBarButtonItem = backButtonItem;
    [backButtonItem release];
    
    //创建设计botton
    UIButton* buttonfinish = [UIButton buttonWithType:UIButtonTypeCustom];
	buttonfinish.frame = CGRectMake(30, 310, 122, 40);
    [buttonfinish setBackgroundImage:[UIImage imageNamed:@"btn_done_normal.png"] forState:UIControlStateNormal];
    [buttonfinish setBackgroundImage:[UIImage imageNamed:@"btn_done_press.png"] forState:UIControlStateHighlighted];
	[buttonfinish setTitle:@"设计完毕" forState:UIControlStateNormal];
    buttonfinish.titleLabel.font = [UIFont boldSystemFontOfSize:16];
	[buttonfinish addTarget:self action:@selector(finishButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:buttonfinish];
    
    //创建下一天botton
    UIButton* buttonNext = [UIButton buttonWithType:UIButtonTypeCustom];
	buttonNext.frame = CGRectMake(160, 310, 122, 40);
    [buttonNext setBackgroundImage:[UIImage imageNamed:@"btn_done_normal.png"] forState:UIControlStateNormal];
    [buttonNext setBackgroundImage:[UIImage imageNamed:@"btn_done_press.png"] forState:UIControlStateHighlighted];
	[buttonNext setTitle:@"下一天" forState:UIControlStateNormal];
    buttonNext.titleLabel.font = [UIFont boldSystemFontOfSize:16];
	[buttonNext addTarget:self action:@selector(nextButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:buttonNext];
    
    dataPartView1 = [[DataPartView alloc] initWithFrame:CGRectMake(0, 70, 320, 50)];
    dataPartView1.partLabel.text = @"上午： ";
    dataPartView1.delegate = self;
    dataPartView1.elementType = 1;
    [self.view addSubview:dataPartView1];
    [dataPartView1 release];
    
    dataPartView2 = [[DataPartView alloc] initWithFrame:CGRectMake(0, 70+65, 320, 50)];
    dataPartView2.partLabel.text = @"下午： ";
    dataPartView2.delegate = self;
    dataPartView2.elementType = 2;
    [self.view addSubview:dataPartView2];
    [dataPartView2 release];
    
    dataPartView3 = [[DataPartView alloc] initWithFrame:CGRectMake(0, 70+65*2, 320, 50)];
    dataPartView3.partLabel.text = @"晚上： ";
    dataPartView3.delegate = self;
    dataPartView3.elementType = 3;
    [self.view addSubview:dataPartView3];
    [dataPartView3 release];
}

- (void)viewWillAppear:(BOOL)animated
{
    NSMutableDictionary* countDict = [[DataSource shareInstance] getEventCount];
    dataPartView1.eventCountLabel.text = [NSString stringWithFormat:@"%@事件",[countDict objectForKey:@"1"]];

    dataPartView2.eventCountLabel.text = [NSString stringWithFormat:@"%@事件",[countDict objectForKey:@"2"]];
    
    dataPartView3.eventCountLabel.text = [NSString stringWithFormat:@"%@事件",[countDict objectForKey:@"3"]];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return NO;
}

- (void)finishButtonPressed:(id)sender
{
    NSLog(@"finishbutton");
    NSInteger eventCount = 0;
    NSMutableDictionary* countDict = [[DataSource shareInstance] getEventCount];
    eventCount += [[countDict objectForKey:@"1"] intValue];
    eventCount += [[countDict objectForKey:@"2"] intValue];
    eventCount += [[countDict objectForKey:@"3"] intValue];
    if (eventCount > 0) {
        ShowDesiginViewController* showDesignViewController = [[ShowDesiginViewController alloc] init];
        [self.navigationController pushViewController:showDesignViewController animated:YES];
    }
    else {
        UIAlertView* baseAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"还没有编辑任何内容哟~" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [baseAlert show];
        [baseAlert release];
    }
//    [showDesignViewController release];
}

- (void)nextButtonPressed:(id)sender
{
    NSLog(@"nextbutton");
//    NSString* oldDateString = [[DataSource shareInstance] getDateForNow];
//    NSDate* newDate = [[NSDate dateWithDateString:oldDateString] dateByAddingTimeInterval:24*60*60];
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
//    NSString* newDateString = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:newDate]];
//    [[DataSource shareInstance] creatNewDate:newDateString];
//    [self setDateContent:[NSString stringWithFormat:@"第%d天",[[DataSource shareInstance].designtripInfo.datedic count]] date:newDateString];
//
//    NSMutableDictionary* countDict = [[DataSource shareInstance] getEventCount];
//    dataPartView1.eventCountLabel.text = [NSString stringWithFormat:@"%@事件",[countDict objectForKey:@"0"]];
//    
//    dataPartView2.eventCountLabel.text = [NSString stringWithFormat:@"%@事件",[countDict objectForKey:@"1"]];
//    
//    dataPartView3.eventCountLabel.text = [NSString stringWithFormat:@"%@事件",[countDict objectForKey:@"2"]];
    NSInteger eventCount = 0;
    NSMutableDictionary* countDict = [[DataSource shareInstance] getEventCount];
    eventCount += [[countDict objectForKey:@"1"] intValue];
    eventCount += [[countDict objectForKey:@"2"] intValue];
    eventCount += [[countDict objectForKey:@"3"] intValue];
    if (eventCount > 0) {
        [self.navigationController popViewControllerAnimated:NO];
        [delegate touchNextDayButton];
    }
    else {
        UIAlertView* baseAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"今天还没有编辑任何内容哟~" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [baseAlert show];
        [baseAlert release];
    }
}

- (void)setDateContent:(NSString*)title date:(NSString*)date
{
    dateDetailView = [[DateDetailView alloc] initWithFrame:CGRectMake(10, 25, 303, 23)];
    dateDetailView.dateLabel.text = title;
    dateDetailView.dateLabel.font = [UIFont systemFontOfSize:14];
    dateDetailView.dateLabel.backgroundColor = [UIColor clearColor];
    CGRect frame = dateDetailView.detailLabel.frame;
    
    int tempWidth = [date sizeWithFont:[UIFont systemFontOfSize:17.0f]
                                      constrainedToSize:CGSizeMake(9999, 23) lineBreakMode:UILineBreakModeCharacterWrap].width;
    
    dateDetailView.detailLabel.frame = CGRectMake(dateDetailView.frame.size.width - tempWidth - 10, frame.origin.y, tempWidth+10, frame.size.height);
    dateDetailView.detailLabel.text = date;
    dateDetailView.detailLabel.font = [UIFont boldSystemFontOfSize:17];
    dateDetailView.detailLabel.textColor = [UIColor colorWithRed:252/255.0 green:100/255.0 blue:22/255.0 alpha:1];
    dateDetailView.detailLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:dateDetailView];
}

- (void)backButtonPress:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
//    [self removeFromParentViewController];
}

- (void)touchUpInsideEditViewCell:(NSString*)IndexText button:(NSInteger)buttontype elementType:(ElementType)type
{
    DateAddEventViewController* dateAddEventViewController = [[DateAddEventViewController alloc] init];
    [[DataSource shareInstance] setDatePart:[NSString stringWithFormat:@"%d",type]]; 
    [dateAddEventViewController setDateContent:dateDetailView.dateLabel.text date:[NSString stringWithFormat:@"%@，%@",dateDetailView.detailLabel.text,IndexText]];
    if ([DataSource shareInstance].defaultCityInfo && [[DataSource shareInstance] getCityId] == nil) {
        [[DataSource shareInstance] setCityinfo:[DataSource shareInstance].defaultCityInfo];
    }
    [dateAddEventViewController updateAllEvent];
    //CustomTabBarController* tabarController = (CustomTabBarController*)self.tabBarController;
    [self.navigationController pushViewController:dateAddEventViewController animated:YES];
//    [dateAddEventViewController release];
}
@end
